
package ImmovableIndia;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.TimeZone;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

/**
 * <p>
 * ConnectionPool contains all the database connection to Fema application.
 */
public class ConnectionPool {

	/**
	 * Connection instance
	 */
	private Connection con = null;

	//static DbConnectionBroker myBroker;

	//static DbConnectionBroker mySunBroker;

	static int counter = 0;

	static int medHSCounter = 0;

	/**
	 * Constructs Populator with no parameters and creates a ConnectionPool Object.
	 * 
	 * @see ConnectionPool
	 */

	public ConnectionPool() throws SQLException, Exception {

		 //con = this.getConnection();
	}

	/**
	 * returns the connection object from the pool
	 * 
	 * @return Connection
	 */

	public Connection getConnection() throws SQLException, Exception {

		try {

			Context initContext = new InitialContext();
			Context envContext = (Context) initContext.lookup("java:/comp/env");
			BasicDataSource envContext1 = (BasicDataSource)initContext.lookup( "java:comp/env/jdbc/fema" );  // from Tomcat's server.xml
			//envContext1.
		      System.out.println("myProp----->"+envContext1);
			DataSource ds = (DataSource) envContext.lookup("jdbc/fema");
			con = ds.getConnection();
			counter++;
			System.out.println("Connection " + counter + " to Fema database created on " + now());
		} catch(Exception e) {

			System.out.println("Connection Problem" + e);
		}

		return con;
	}

	/**
	 * releases the connection object.
	 * 
	 * 
	 */

	public void releaseConnection(Connection con) throws SQLException, Exception {

		try {

			if(con != null) {

				con.close();
				System.out.println("Connection " + counter + " to Fema database closed on " + now());
				counter--;
				con = null;
			}
		} catch(Exception e) {

			System.out.println("Problem in Closing" + e);
		}
	}

	public String now() {

		/*
		 * * on some JDK, the default TimeZone is wrong * we must set the TimeZone manually!!! * Calendar cal =
		 * Calendar.getInstance(TimeZone.getTimeZone("EST"));
		 */
		Calendar cal = Calendar.getInstance(TimeZone.getDefault());

		String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
		/*
		 * * on some JDK, the default TimeZone is wrong * we must set the TimeZone manually!!! *
		 * sdf.setTimeZone(TimeZone.getTimeZone("EST"));
		 */
		sdf.setTimeZone(TimeZone.getDefault());

		return sdf.format(cal.getTime());
	}
	/*
	 * * Releases the Result set and Statement resources.
	 */
	public void releaseResources(PreparedStatement pst, ResultSet rs) throws Exception {

		releaseResources(rs);
		releaseResources(pst);
	}

	public void releaseResources(PreparedStatement pst) throws Exception {

		if(pst != null) {

			pst.close();
			pst = null;
		}
	}
	public void releaseResources(Statement st) throws Exception {

		if(st != null) {

			st.close();
			st = null;
		}
	}
	public void releaseResources(ResultSet rs) throws Exception {

		if(rs != null) {

			rs.close();
			rs = null;
		}
	}

	/*public CLOB getCLOB(String xml_Content, PreparedStatement pst) throws SQLException {

		CLOB tempClob = null;
		try {
			Connection oracleConn = getOracleConnection(pst.getConnection());
			// If the temporary CLOB has not yet been created, create new
			tempClob = CLOB.createTemporary(oracleConn, false, CLOB.DURATION_SESSION);

			// Open the temporary CLOB in readwrite mode to enable writing
			tempClob.open(CLOB.MODE_READWRITE);
			// Get the output stream to write
			Writer tempClobWriter = tempClob.getCharacterOutputStream();
			// Write the data into the temporary CLOB
			tempClobWriter.write(xml_Content);

			// Flush and close the stream
			tempClobWriter.flush();
			tempClobWriter.close();

			// Close the temporary CLOB 
			tempClob.close();
		} catch(SQLException sqlexp) {
			tempClob.freeTemporary();
			sqlexp.printStackTrace();
		} catch(Exception exp) {
			tempClob.freeTemporary();
			exp.printStackTrace();
		}
		return tempClob;
	}*/

	/*public Connection getOracleConnection(Connection conn) {

		return ((DelegatingConnection)conn).getInnermostDelegate();

	}*/
}
